// pages/wuliurongqiguanli/nav_problem_search/problem_search.js
const mybehavior = require("../../../behaviors/requests")
Page({
  /**
   * 页面的初始数据
   */
  behaviors: [mybehavior],
  data: {
    baseUrl: "https://logisticsengineering.gree.com/container_manage_api/container_manage",
    token: "",
    username: "",
    activeName: [],
    data_list: [],
    sp_show: false,
    ss_show: false,
    _id: "",
    reason: "",
    ss_reason: "",
    result: "",
    qsyj_options: ["yes", "no"],
    agree_btn: "default",
    disagree_btn: "default",
    detail_show: false,
    detail_list: []

  },
  cancel_ss_dialog() {
    this.setData({
      ss_reason: "",
      ss_show: false
    })
  },
  onChange(event) {
    this.setData({
      activeName: event.detail,
    });
  },
  onAgree() {
    this.setData({
      agree_btn: "danger",
      result: "yes",
      disagree_btn: "default",
    })
  },
  onDisgree() {
    this.setData({
      agree_btn: "default",
      result: "no",
      disagree_btn: "danger",
    })
  },
  //申诉弹窗确定按钮
  ssConfirm(event) {
    const that = this;
    if (this.data.ss_reason == "") {
      wx.showToast({
        title: "理由不能为空！",
      })
    }
    else {
      const params = {
        id:that.data._id,
        reason:that.data.ss_reason
      }
      this.post(that.data.baseUrl + '/problem_appeal',params).then((res) => {
        console.log(res)
        that.setData({
          ss_reason:""
        })
        that.getDatas();
      })
    }
  },
  // 审批弹框确定按钮
  onSpConfirm() {
    if (this.data.reason == "" || this.data.result == "") {
      wx.showToast({
        title: '不能为空',
        icon: "error"
      })
    } else {
      var url = this.data.baseUrl + "/problem_judge"
      var data = {
        id: this.data._id,
        reason: this.data.reason,
        result: this.data.result,
        token: this.data.token,
        user_email: this.data.user_email,
      }
      console.log(data);
      this.post(url, data).then(res => {
        console.log("审批返回结果", res)
        if (res.code == 20000) {
          wx.showToast({
            title: res.msg,
            icon: "success"
          });
          this.getDatas()
        } else {
          wx.showToast({
            title: res.msg,
            icon: "error"
          })
        }
      }).catch(err => {
        wx.showToast({
          title: err.msg,
          icon: 'error'
        })
      })

    }

  },
  // 审批弹框关闭
  onSpClose() {
    this.setData({
      sp_show: false,
      _id: "",
      reason: "",
      result: "",
      agree_btn: "default",
      disagree_btn: "default",
    });
  },
  // 点击申诉按钮
  showSSDialog(e) {
    console.log(e.target.dataset.row_data);
    var row = e.target.dataset.row_data
    this.setData({
      _id: row._id,
      ss_show: true,
    })
  },

  // 点击审批按钮
  onApprove(e) {
    console.log(e.target.dataset.row_data);
    var row = e.target.dataset.row_data
    this.setData({
      _id: row._id,
      row_data: row,
      sp_show: true,
    })


  },
  // 预览照片
  previewPhoto(e) {
    console.log(e.currentTarget.dataset.row_data)
    var img_url = 'https://logisticsengineering.gree.com/container_manage_api/container_manage/fs_download?file_id=' + e.currentTarget.dataset.row_data
    var u = []
    u.push(img_url)
    wx.previewImage({
      current: img_url,
      urls: u,
    })
  },
  // 打开补充问题详情
  showDetail(e) {
    console.log(e.currentTarget.dataset.row_data.live_record)
    this.setData({
      detail_show: true,
      detail_list: e.currentTarget.dataset.row_data.live_record
    })
  },
  // 详情弹窗关闭
  detailConfirm() {
    this.setData({
      detail_show: false,
      detail_list: ""
    })
  },

  onClose() {
    if (this.data.token == "") {
      this.setData({
        show: true
      })
    } else {
      this.setData({
        show: false
      })
    }

  },

  // 查询数据
  getDatas() {
    var url = this.data.baseUrl + "/problem_view"
    var data = {}
    this.post(url, data).then(res => {
      console.log("返回结果", res)
      if (res.code == 20000) {
        this.setData({
          data_list: res.data
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: "error"
        })
      }
    }).catch(err => {
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getDatas()

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})